
#include "itemSelector.h"



ItemSelector::ItemSelector()
{
	slots = 0;
	for (int n = 0; n < 8; n++)
	{
		items[n] = ITEM_NOITEM;
		hovered[n] = 1;
	}
}

ItemSelector::~ItemSelector()
{

}

void ItemSelector::Initialize(Graphics *g, Input *i, Audio *a)
{
	graphics = g;
	input = i;
	audio = a;

	// Initialize texture
	if (!buttonTexture.initialize(graphics,ITEMSELECTORBUTTON_IMAGE))
		throw(GameError(gameErrorNS::FATAL_ERROR, "Error initializing button texture"));

	// Initialize butons
	button.initialize(graphics, 30, 90, 2, &buttonTexture);


}


void ItemSelector::draw()
{
	if (activated)
	{
		float oX = button.getX();
		float oY = button.getY();
		float oR = button.getRadians();
		draw8();
		button.setX(oX);
		button.setY(oY);
		button.setRadians(oR);
		for (int n = 0; n < slots; n++)
			imageOverlays[n].draw();
	}
}

void ItemSelector::draw8()
{
	//button.setCurrentFrame(1);
	button.setX(centerX - button.getWidth()/2);
	button.setY(centerY - button.getHeight()/2);
	
	button.setCurrentFrame(hovered[2]);
	button.draw();
	
	button.setCurrentFrame(hovered[5]);
	button.setRadians(PI * 1/4);
	button.draw();

	button.setCurrentFrame(hovered[1]);
	button.setRadians(PI * 2/4);
	button.draw();

	button.setCurrentFrame(hovered[7]);
	button.setRadians(PI * 3/4);
	button.draw();

	button.setCurrentFrame(hovered[3]);
	button.setRadians(PI);
	button.draw();

	button.setCurrentFrame(hovered[6]);
	button.setRadians(PI * 5/4);
	button.draw();

	button.setCurrentFrame(hovered[0]);
	button.setRadians(PI * 6/4);
	button.draw();

	button.setCurrentFrame(hovered[4]);
	button.setRadians(PI * 7/4);
	button.draw();
	
}

void ItemSelector::addItem(int item)
{
	for (int n = 0; n <8; n++)
	{
		if (items[n] == ITEM_NOITEM)
		{
			items[n] = item;
			textureManagers[n].initialize(graphics, itemNToFileName(item));
			imageOverlays[n].initialize(graphics,0,0,0,&textureManagers[n]);
			return;
		}
	}
}



void ItemSelector::removeItem(int pos)
{
	items[pos] = ITEM_NOITEM;
}

const char *ItemSelector::itemNToFileName(int itemN)
{
	switch (itemN)
		case 1 : return BUBBLE_IMAGE;	
}

void ItemSelector::updateImageOverlaysPositions()
{
	imageOverlays[0].setX(centerX - 80);
	imageOverlays[0].setY(centerY - 20);
}

void ItemSelector::update(float frameTime)
{
	update8();

}

void ItemSelector::update8()
{
	for (int n = 0; n < slots; n++)
	{
		hovered[n] = 1;
	}

	VECTOR2 v1;
	v1.x = centerX;
	v1.y = centerY;

	VECTOR2 v2;
	v2.x = input->getMouseX();
	v2.y = input->getMouseY();

	if (v1 != v2)
	{
		VECTOR2 v = v2 - v1;

		angle = atan2(v.y, v.x);
	
		if ( ((-PI * 7/8) > angle) || (angle > (PI * 7/8)) )
			hovered [0] = 0;
	
		if ( ((-PI * 1/8) < angle) && (angle < (PI * 1/8)) )
			hovered [1] = 0;

		if ( ((-PI * 5/8) < angle) && (angle < (-PI * 3/8)) )
			hovered [2] = 0;

		if ( ((PI * 3/8) < angle) && (angle < (PI * 5/8)) )
			hovered [3] = 0;

		if ( ((-PI * 7/8) < angle) && (angle < (-PI * 5/8)) )
			hovered [4] = 0;

		if ( ((-PI * 3/8) < angle) && (angle < (-PI * 1/8)) )
			hovered [5] = 0;

		if ( ((PI * 5/8) < angle) && (angle < (PI * 7/8)) )
			hovered [6] = 0;

		if ( ((PI * 1/8) < angle) && (angle < (PI * 3/8)) )
			hovered [7] = 0;
	}

	

}


void ItemSelector::activate()
{
	activated = true;
	selectedItem = ITEM_NOITEM;
	setCenterX(input->getMouseX());
	setCenterY(input->getMouseY());

}

void ItemSelector::deactivate()
{
	activated = false;
	for (int n = 0; n < slots; n++)
	{
		//if (buttons[n].isHovered())
			selectedItem = items[n];
	}
}